//
//  myCollectViewController.m
//  LT
//
//  Created by dllo on 16/5/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "myCollectViewController.h"
#define WITDH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "ZhuanTDetailViewController.h"
#import "ZhuanTModel.h"


static NSString *const collectCell = @"collectCell";

@interface myCollectViewController ()
@property(nonatomic,strong)UITableView *collectionTableView;

//用来装已经收藏的东西的数组
@property(nonatomic,strong)NSMutableArray *collectArray;
@property (nonatomic, strong) NSMutableArray *allArray;


@end

@implementation myCollectViewController

- (void)viewWillAppear:(BOOL)animated
{
    NSString *userStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    NSMutableArray *array = [[DataBaseHandle shareDataBaseHandle] selectZhuanT:userStr withCompare:NO];
    self.collectArray = [NSMutableArray arrayWithArray:array];
   
    NSMutableArray *allArr = [[DataBaseHandle shareDataBaseHandle] selectZhuanT:userStr withCompare:YES];
    self.allArray = [NSMutableArray arrayWithArray:allArr];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    //tableView
    self.collectionTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WITDH, HEIGHT) style:UITableViewStylePlain];
    //self.collectionTableView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.collectionTableView];
    
    self.collectionTableView.delegate = self;
    self.collectionTableView.dataSource = self;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"goback"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    //注册
    [self.collectionTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:collectCell];
    [self.collectionTableView reloadData];

}

//cell的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//逻辑:如果没有用户登录,这里面的东西是不显示的
    //首先调用userdafault表格
    ZhuanTDetailViewController *zhuanTDetail = [[ZhuanTDetailViewController alloc]init];
    ZhuanTModel *model = [self.allArray objectAtIndex:indexPath.row];
    zhuanTDetail.ZhuanTString = model.url;//url没传过来
    [self.navigationController pushViewController:zhuanTDetail animated:YES];
    
    
    

}


//左按钮
-(void)leftButtonAction{

[self dismissViewControllerAnimated:YES completion:^{
    
    
}];

}


//cell的行数由collectArray中收藏的数量来决定
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.allArray.count;
    
    
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80;

}

//cell的显示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:collectCell];
    cell.textLabel.text = [self.allArray[indexPath.row] title];
    return cell;

}

//是否删除
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}


//行编辑风格
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ZhuanTModel *model = [self.allArray objectAtIndex:indexPath.row];
        [[DataBaseHandle shareDataBaseHandle]ZhuanTDeleteTable:model.title];
        [self.allArray removeObjectAtIndex:indexPath.row];
        [self.collectionTableView reloadData];
    }
    
    
}


//删除行
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
